function genElem(elem) {
    var elem = '<tr><td>' + elem.name + '</td>'
        + '<td><span class = "lessNum">-</span><span class="totalNum" id="allNum">' + elem.count
        + '</span ><span id = "thisId">' + elem.id + '</span><span class="moreNum">+</span ></td>'
        + '<td>' + elem.price + '</td></tr>';
    return elem;
}

function moreNumBtnEvent(event) {
    var elem = $(event.target).prev().prev();
    var id = parseInt($(event.target).prev().text());
    var count = parseInt(elem.text()) + 1;
    elem.text(count);
    for (var i = 0; i < json_Data.length; i++) {
        if (json_Data[i].id === id) {
            json_Data[i].count = count;
            break;
        }
    }
}

function lessNumBtnEvent() {
    var elem = $(event.target).next();
    var id = parseInt($(event.target).next().next().text());
    var count = parseInt(elem.text());
    if (count > 0) {
        count--
        elem.text(count);
        for (var i = 0; i < json_Data.length; i++) {
            if (json_Data[i].id === id) {
                json_Data[i].count = count;
                break;
            }
        }
    }
}

//点击购买后的回调函数
function addOrderCallback(message) {
    $("#loading-result").text(message);
    $("#loading").css("display", "");
    setTimeout(function () {
        $("#loading").hide();
    }, 1000);
}

function accountBtnEvent() {
    $("#dialog").css("display", "");

    $('[data-action="cancel"]').unbind();
    $('[data-action="confirm"]').unbind();

    $('[data-action="cancel"]').click(function () {
        $("#dialog").hide();
    });
    $('[data-action="confirm"]').click(function () {
        $("#dialog").hide();
        //todo 检查输入的合法性
        $.ajax({
            type: 'post',
            url: '/addOrder.json',
            data: JSON.stringify(json_Data),
            cache: false,
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                if (data.code === 200 && data.data) {
                    addOrderCallback("购买成功");
                    window.location.assign("/account");
                } else if (data.code === 200 && !data.data) {
                    addOrderCallback("购买数量为零");
                } else if (data.code === 300) {
                    addOrderCallback("请登录买家用户");
                } else if (data.code === 400) {
                    addOrderCallback("早已购买或者无此商品");
                } else {
                    addOrderCallback("服务器错误");
                }
            },
            error: function (data) {
                alert("添加购物车失败,请重试!");
            }
        });
    });
}

function backBtnEvent() {
    window.location.assign("/");
}

(function () {
    var trElem = $("#newTable");
    for (var i = 0; i < json_Data.length; i++) {
        trElem.append(genElem(json_Data[i]));
    }

    //是否购买的选择框
    $("body").append($(dialog));
    $("#dialog-content").text("确认购买？");
    $("#dialog").css("display", "none");

    //购买成功或失败的消息框
    $("body").append($(loading));
    $("#loading").hide();

    $(".lessNum").on("click", lessNumBtnEvent);
    $(".moreNum").on("click", moreNumBtnEvent);
    $("#back").on("click", backBtnEvent);
    $("#Account").on("click", accountBtnEvent);
})();